var AddThis_Toolbar = {

    _resizeTrigger: false,
    _resizing: false,
	    
    init : function() {
        AddThis_Util.dbg("Toolbar.init: initting...");
        document.getElementById("at_tbShare").width = ""; 
        AddThis_Toolbar.prepare();
        AddThis_Util.dbg("Toolbar.init: done.");
    },
    
	toggleVisibility : function () {
		AddThis_Util.dbg("Toolbar.toggleVisibility: entered...");
		var visible = AddThis_Toolbar.isVisible();
		var newCollapsed = !visible;
		AddThis_Util.dbg("Toolbar.toggleVisibility: Currently visible: " + visible + ", setting to: " + newCollapsed);
        AddThis_Toolbar.setVisibility(newCollapsed);
		AddThis_Util.dbg("Toolbar.toggleVisibility: done.");
	},
	
	isVisible : function(doc) {
        if (typeof doc == "undefined") doc = document;
        var tb = doc.getElementById("at_toolbar");
        var visible = tb ? !(tb.collapsed ||
                             tb.hidden ||
                             getComputedStyle(doc.getElementById("at_toolbar"),null).getPropertyValue('display')=='none') :
                            false;
		AddThis_Util.dbg("Toolbar.isVisible: " + visible);
		return visible;
	},
	
	setVisibility : function(visible, doc) {
        if (typeof doc == "undefined") doc = document;
		AddThis_Util.dbg("Toolbar.setVisibility: " + visible);
		var bar = doc.getElementById("at_toolbar");
		bar.collapsed = !visible;
        doc.persist(bar.id, "collapsed");
		AddThis_Util.dbg("Toolbar.setVisibility: collapsed now = " + bar.collapsed);
	},
    
    prepare: function(doc)
    {
        AddThis_Util.dbg("Toolbar.prepare: entered...");
        
        if (typeof doc == "undefined") doc = document;
        
        var tb_sc = doc.getElementById("at_tbShareContainer");
        
        AddThis_Toolbar.setServices(doc);
        AddThis_Toolbar.setButtonStyle(doc);
        
        AddThis_Util.dbg("Toolbar.prepare: done");
    },
    
    setServices: function(doc) {
        AddThis_Util.dbg("Toolbar.setServices: entered...");
        
        if (typeof doc == "undefined") doc = document;
        var container = doc.getElementById('at_tbShareContainer');
        var overflow = doc.getElementById('at_tbShareOverflowMenu');
        
        
        var menuitems = container.getElementsByTagName('toolbarbutton');
        while (menuitems.length > 0) container.removeChild(menuitems[0]);
        
        menuitems = overflow.getElementsByTagName('menuitem');
        while (menuitems.length > 0) overflow.removeChild(menuitems[0]);
        
        var serviceId, items=0, first=true;
            activeServices = AddThis_Util.getActiveServicesObject();
        for (serviceId in activeServices)
        {
            container.insertBefore(AddThis_Toolbar._createServiceButton(serviceId),null);
            items++;
        }
        
        
        doc.getElementById("at_tbShare").className = Math.random().toString();
        
        AddThis_Toolbar.resizeDelay();
        AddThis_Util.dbg("Toolbar.setServices: done");
        
    },
    
    setButtonStyle: function(doc) {
        AddThis_Util.dbg("Toolbar.updateButtonStyle: entered...");
        
        var style = AddThis_Util.getStringPref('extensions.addthis.toolbar.buttonstyle', 'icons');
        
        if (typeof doc == "undefined") doc = document;
        var tb = doc.getElementById("at_toolbar");
        
        tb.setAttribute("mode", style);
        
        if (style=="icons") style="pictures";
        tb.setAttribute("buttonstyle", style);
        
        AddThis_Toolbar.resizeDelay();
        AddThis_Util.dbg("Toolbar.updateButtonStyle: done");
    },
    
    resize: function() {
        AddThis_Util.dbg("Toolbar.resize: entered...");
        
        if (this._resizeTrigger !== false) clearTimeout(this._resizeTrigger);
        
        if (this._resizing) return; 
        
        var doc = document;
        if (AddThis_Toolbar.isVisible(doc)) 
        {
            AddThis_Util.dbg("Toolbar.resize: started..."); 
            this._resizing = true;
            
            var sp = doc.getElementById("at_tbSpacer");
            if (!sp) {
                var bw = AddThis_Preferences._getBrowser();
                doc = bw.document;
                sp = doc.getElementById("at_tbSpacer");
            }
            
            var sc = doc.getElementById("at_tbShareContainer"),
                oc = doc.getElementById("at_tbShareOverflowMenu");
            
            AddThis_Util.dbg('spacer width: ' + sp.boxObject.width);
            
            
            if (sp.boxObject.width > 0 && oc.firstChild) {
                
                AddThis_Toolbar._grow(sp,sc,oc);
                AddThis_Toolbar._shrink(sp,sc,oc); 
            } else if (sp.boxObject.width <= 0) {
                AddThis_Toolbar._shrink(sp,sc,oc);
            }
            
            var o = doc.getElementById("at_tbShareOverflow");
            o.hidden = (oc.firstChild) ? false : true;
        }
        
        this._resizing = false;
        AddThis_Util.dbg("Toolbar.resize: done");

    },
    
    _grow: function(sp,sc,oc) {
        AddThis_Util.dbg("Toolbar._grow: entered...");
        
        var mi;
        
        while (sp.boxObject.width > 0 && oc.firstChild)
        {
            mi = oc.removeChild(oc.firstChild);
            sc.appendChild(AddThis_Toolbar._createServiceButton(mi.getAttribute("serviceid")));
        }
        
        AddThis_Util.dbg("Toolbar._grow: done...");
    },
    
    _shrink: function(sp,sc,oc) {
        AddThis_Util.dbg("Toolbar._shrink: entered...");
        
        var tbb;
        
        while (sp.boxObject.width <= 0 && sc.lastChild)
        {
            tbb = sc.removeChild(sc.lastChild);
            oc.insertBefore(AddThis_Sharing._createMenuitem(tbb.getAttribute("serviceid")),oc.firstChild);
        }
        
        AddThis_Util.dbg("Toolbar._shrink: done...");
    },
    
    resizeDelay: function()
    {
        if (AddThis_Toolbar.isVisible(document))
        {
            AddThis_Util.dbg("Toolbar.resizeDelay triggered");
            if (this._resizeTrigger !== false) clearTimeout(this._resizeTrigger);
            this._resizeTrigger = setTimeout("AddThis_Toolbar.resize()",500);
        }
    },
    
    _createServiceButton: function(serviceId)
    {
        var newItem = document.createElement("toolbarbutton");
        newItem.setAttribute("serviceid",serviceId);
        newItem.setAttribute("label",AddThis_Constants.services[serviceId].title);
        newItem.setAttribute("tooltiptext",AddThis_Constants.services[serviceId].title);
        newItem.setAttribute("image", "chrome://addthis/skin/services/"+serviceId+".gif");
        newItem.setAttribute("oncommand", "AddThis_Sharing.invokeService('"+serviceId+"');event.stopPropagation();");
        return newItem;
    }
    
    
}
